package com.example.shop.controller;

import com.example.shop.bean.CollectBean;
import com.example.shop.mapper.CollectMapper;
import com.example.shop.util.NotNullUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class CollectController {
    @Autowired
    CollectMapper  collectMapper;
    //增加
    @ResponseBody
    @RequestMapping("/addcollect")
    public String addCollect(CollectBean bean){
        List<CollectBean> collectL;
        collectL=collectMapper.listByUidPid(bean.uid,bean.pid);
        if(collectL.size()<=0){
            try{
                collectMapper.insert(bean);
                return "收藏成功!";
            }catch (Exception e){
                System.out.println("收藏商品失败");
                return "收藏失败!";
            }
        }else{
            return "已经收藏过了!";
        }
    }
    //取消收藏
    @ResponseBody
    @RequestMapping("/cancelcollect")
    public String cancelCollect(CollectBean bean){
        try{
            collectMapper.cancelCollectByUidPid(bean.uid,bean.pid);
            return "已取消收藏";
        }catch (Exception e){
            System.out.println("取消收藏失败");
            return "取消收藏失败";
        }
    }

    //判断是否收藏
    @ResponseBody
    @RequestMapping("/ifcollected")
    public String ifCollected(CollectBean bean){
        if(NotNullUtil.isBlank(bean)){
            //只要有一个空 就进来了
            return "0";
        }
        List<CollectBean> collectL;
        collectL=collectMapper.listByUidPid(bean.uid,bean.pid);
        if(collectL.size()>0){
            return "1";
        }else{
            return "0";
        }
    }

    @ResponseBody
    @RequestMapping("/collectlist")
    public List<CollectBean> collectList(Integer uid){
        if(uid==null){
            return null;
        }
        List<CollectBean> collectL;
        collectL=collectMapper.collectList(uid);
        return collectL;
    }

    @ResponseBody
    @RequestMapping("/collectlist2")
    public List<CollectBean> collectList2(){

        List<CollectBean> collectL;
        collectL=collectMapper.collectList2();
        return collectL;
    }
}
